<!--
  author: tangcheng_cd
  create by: 2018/5/21
  descr: 用户管理表格Grid
-->
<template>
  <div id="user-mana-grid">
    <el-table
      ref="userGrid"
      @row-click="rowClicked"
      v-loading="tableLoading"
      highlight-current-row
      :data="pageData.list"
      class="user-mana-table">
      <!--<el-table-column-->
        <!--type="selection"-->
        <!--width="40">-->
      <!--</el-table-column>-->
      <el-table-column type="expand">
        <template slot-scope="props">
          <el-button type="text" @click="changeUserStatus(props.row)"
                     size="small">{{['锁定此用户', '解锁此用户'][props.row.status - 1]}}</el-button>
          <el-button type="text" @click="resetPassword(props.row)"
                     size="small">重置密码</el-button>
          <el-form label-position="left" inline class="form-table-expand">
            <el-form-item label="账号">
              <span>{{ props.row.code }}</span>
            </el-form-item>
            <el-form-item label="姓名">
              <span>{{ props.row.name }}</span>
            </el-form-item>
            <el-form-item label="邮件">
              <span>{{ props.row.email }}</span>
            </el-form-item>
            <el-form-item label="电话">
              <span>{{ props.row.mobile }}</span>
            </el-form-item>
            <el-form-item label="状态">
              <span>{{ statusItems[props.row.status] }}</span>
            </el-form-item>
            <el-form-item label="密码过期日">
              <span>{{ formatDate(props.row.failTime) }}</span>
            </el-form-item>
          </el-form>
        </template>
      </el-table-column>
      <el-table-column
        prop="code"
        label="账号"
        sortable
        width="180" >
      </el-table-column>
      <el-table-column
        prop="name"
        label="姓名"
        width="180">
      </el-table-column>
      <el-table-column
        prop="email"
        label="邮件">
      </el-table-column>
      <el-table-column
        :formatter="columnFormatter"
        prop="status"
        label="状态">
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="search.pageNo"
      :page-sizes="[5, 10, 15, 20]"
      :page-size="search.pageSize"
      layout="total, prev, pager, next"
      :total="pageData.total">
    </el-pagination>
  </div>
</template>

<script type="text/ecmascript-6">
  import api from '../../../api/url' // 获取用户
  import ajax from '../../../api/ajax'
  import {formatDate} from 'element-ui/packages/date-picker/src/util'

  export default {
    name: 'user-mana-grid',
    data () {
      return {
        tableLoading: false, // 数据是否加载中
        search: {
          type: Object,
          pageNo: 1, // 当前页号
          pageSize: 5, // 每页显示记录数
          orgId: null, // 机构id
          keyword: null // 关键字
        },
        pageData: Object, // 当前表格页数据
        statusItems: { // 状态枚举值
          1: '正常',
          2: '锁定'
        },
        currentRow: null // 当前行
      }
    },
    methods: {
      formatDate (...arg) {
        return formatDate(...arg)
      },
      // 列格式化定制
      columnFormatter (row, column) {
        let result = ''
        switch (column['property']) {
          case 'status': // 状态格式化
            result = this.statusItems[row.status] || ''
            break
        }
        return result
      },
      // 行单击
      rowClicked (row, el) {
        this.currentRow = row
      },
      // 页记录数更改
      handleSizeChange (pageSize) {
        this.search.pageNo = 1 // 重置到第一页
        this.search.pageSize = pageSize
        this.getPageData()
      },
      // 当前页切换
      handleCurrentChange (pageNo) {
        this.search.pageNo = pageNo
        this.getPageData()
      },
      // 通过关键字搜索
      getDataByKeyword (keyword) {
        this.search.keyword = keyword
        this.getData()
      },
      // 获取页面数据
      getData () {
        // 角色数据加载中
        this.tableLoading = true
        ajax.post(api.SEARCH_USER_BY_ORGID, this.search).then(res => {
          this.pageData = res.data
          // 角色数据加载完毕
          this.tableLoading = false
        }).catch(e => {
          console.error('获取用户数据发生错误！')
        })
      },
      // 获取所有的角色数据
      getPageData (param = {
        orgId: null
      }) {
        this.search.orgId = param.orgId
        this.getData()
      },
      // 获取当前选中数据
      getCurrentNode () {
        return this.currentRow
      },
      // 清除当前选择
      clearCurrentNode () {
        this.currentRow = null
      },
      // 设置用户状态
      changeUserStatus (user) {
        this.$emit('changeUserStatus', user)
      },
      // 重设密码
      resetPassword (user) {
        this.$emit('resetPassword', user)
      }
    }
  }
</script>

<style lang="stylus" rel="stylesheet/stylus">
  #user-mana-grid
    .user-mana-table
      padding-bottom 0px
      .form-table-expand
        font-size 0
      .form-table-expand label
        width 90px
        color #99a9bf
      .form-table-expand .el-form-item
        margin-right 0
        margin-bottom 0
        width 50%
</style>
